package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.background.common.R;
import com.reggie.background.common.ThreadLocalUtils;
import com.reggie.background.entity.ShoppingCart;
import com.reggie.background.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(ThreadLocalUtils.getThreadLocalValue());
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        ShoppingCart scResult = shoppingCartService.getOne(qw);
        if (scResult!=null) {
            scResult.setNumber(scResult.getNumber()+1);
            shoppingCartService.updateById(scResult);
            return R.success(scResult);
        }else {
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }


    }
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> list =  shoppingCartService.list();
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> query=new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId,ThreadLocalUtils.getThreadLocalValue());
        shoppingCartService.remove(query);
        return R.success("清空购物车成功！");
    }
    @PostMapping("/sub")
    public  R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setUserId(ThreadLocalUtils.getThreadLocalValue());
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        ShoppingCart scResult = shoppingCartService.getOne(qw);
        if (scResult.getNumber()==1){
            shoppingCartService.removeById(scResult.getId());
            return R.success(new ShoppingCart());
        }else {
            scResult.setNumber(scResult.getNumber()-1);
            shoppingCartService.updateById(scResult);
            return R.success(scResult);
        }


    }
}
